/**
 * Created by Tiany on 2017/3/13 0013.
 */
"use strict";
import Vue from "vue";
import VueResource from "vue-resource";
import cryptoJs from 'crypto-js'
Vue.use(VueResource);

export default {
  install: function (Vue, options) {
    // var domain = "http://193.168.5.33:80/yjs_app/";
    // var domain = sessionStorage.getItem("localUrl");
    // var domain = "/api/yjs_app/";//联调测试
    // var domain = "http://rest.apizza.net/mock/a9f28f2b3216758a7a2467410f34c438/";//模拟测试
    // let domain = 'http://192.168.13.31:18080/yjs_app/';
    // let domain = 'http://193.168.5.123:7720/yjs_app/';
    var desKey = "yjs_trade_app";
    //加密
    function encryptByDES(message, key){
      var keyHex = cryptoJs.enc.Utf8.parse(key)
      var option = { mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 }
      var encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
      return encrypted.ciphertext.toString()
    };
    //解密
    function decryptByDESModeEBC(ciphertext,key){
      var keyHex = cryptoJs.enc.Utf8.parse(key)
      var decrypted = cryptoJs.DES.decrypt(
        {
          ciphertext: cryptoJs.enc.Hex.parse(ciphertext)
        },
        keyHex,
        {
          mode: cryptoJs.mode.ECB,
          padding: cryptoJs.pad.Pkcs7
        }
      )
      return decrypted.toString(cryptoJs.enc.Utf8)
    };

    Vue.prototype.$resGet = function(url,data){
      if(sessionStorage.getItem("localUrl")){
        return Vue.http.get(sessionStorage.getItem("localUrl") + url, {params:{data:encryptByDES(JSON.stringify(data),desKey)}},{timeout:60000}).then(res => {
          var result = decryptByDESModeEBC(res.bodyText,desKey);
          return JSON.parse(result);
        })
      }
      // return Vue.http.get(domain + url, {params:{data:encryptByDES(JSON.stringify(data),desKey)}},{timeout:60000}).then(res => {
      //   var result = decryptByDESModeEBC(res.bodyText,desKey);
      //   console.log(JSON.parse(result))
      //   return JSON.parse(result);
      // })
      // return Vue.http.get(domain + url, {params:data},{timeout:60000}).then(res => res.data);
    };

    Vue.prototype.$resPost = function(url,data){
        return Vue.http.post(domain + url, data,{timeout:60000}).then(res => res.data);
    };

    Vue.prototype.$xresPost = function(url,data){
      return Vue.http.post(domain + url, data,{emulateJSON:true,timeout:60000}).then(res => res.data)
    };

    Vue.prototype.$dataMonths = function(){
      let  dataArr  = [];
      let data = new Date();
      let year = data.getFullYear();
      let month = data.getMonth();
      if(month == 0){
        month = 12;
        year--;
      }
      let k =0;
      for(let i=month;i>0&&k<12;i--){
        let m = i<10?'0'+i:i;
        k++;
        dataArr.push(year + "-" + m);
        if(i == 1){
          i = 13;
          year--;
        }
      }
      return dataArr;
    };
    // Vue.prototype.$dataMonths = function(){
    //   var dataArr = [];
    //   var data = new Date();
    //   var year = data.getFullYear();
    //   data.setMonth(data.getMonth()+1, 1)//获取到当前月份,设置月份
    //   for (var i = 0; i < 12; i++) {
    //     data.setMonth(data.getMonth() - 1);//每次循环一次 月份值减1
    //     var m = data.getMonth() + 1;
    //     m = m < 10 ? "0" + m : m;
    //     dataArr.push(data.getFullYear() + "-" + (m))
    //   }
    //   return dataArr;
    // };

    // Vue.prototype.$reportUse = (userId, type, refId) => {
    //   return Vue.http.post("", {
    //     userId,
    //     type,
    //     refId
    //   }, {emulateJSON:true,timeout:30000})
    //   .then(res => res.data)
    //   .then(data => console.log(data));
    // }
    Vue.prototype.$RouterPath = function (path) {
      let winUrl = window.location.href;
      let baseUrl = winUrl.substring(0,winUrl.search(/#/))+"#/";
      window.location.href = baseUrl+path;
    }
  }
};
